<template>
    <div class="channel" v-loading="loading">
        <div class="input-box">
            <el-input
                placeholder="根据标题搜索"
                style="width:180px;"
                @keyup.enter.native="getDataList(1)"
                clearable
                v-model.trim="keyWord">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>

            <el-select
                style="margin-left:16px;width:290px"
                v-model="gameId"
                clearable
                filterable
                @change="getDataList(1)"
                :filter-method="filterMethod"
                :loading="gameLoading"
                placeholder="根据游戏搜索">
                <el-option
                    v-for="item in gameList"
                    :key="item.gameid"
                    :label="item.appName"
                    :value="item.gameid">
                    <span style="float: left">
                        <span>{{ item.appName }}</span>
                        <span v-if="item.status === -1" style="color:red;font-size:12px;margin:0 5px;">下架</span>
                    </span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.gameid }}</span>
                </el-option>
            </el-select>

            <el-button type="primary" class="btn" @click="getDataList(1)">搜索</el-button>
            <el-button type="success" class="btn" @click="goDetail(0)">新建</el-button>
        </div>

        <div class="input-box" style="margin-top:10px;">
            <template v-if="!channelType">
                <span class="select-title">展示样式：</span>
                <el-select
                    @change="getDataList(1)"
                    style="width:150px;"
                    v-model="type">
                    <el-option label="全部" :value="0"></el-option>
                    <el-option :label="item.name" :value="item.type" v-for="(item, index) in typeList" :key="index"></el-option>
                </el-select>
            </template>

            <template v-if="!channelType">
                <span class="select-title">状态：</span>
                <el-select
                    @change="getDataList(1)"
                    style="width:150px;"
                    v-model="status">
                    <el-option label="全部" :value="0"></el-option>
                    <el-option label="首页展示中" :value="3"></el-option>
                    <el-option label="未展示" :value="1"></el-option>
                    <el-option label="暂停" :value="2"></el-option>
                </el-select>
            </template>
        </div>

        <div class="table-box">
            <el-table
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column
                    fixed
                    prop="channelid"
                    label="id">
                </el-table-column>
                <el-table-column
                    prop="label"
                    label="标题">
                    <template slot-scope="scope">
                        <div>{{scope.row.label || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="180"
                    label="频道栏目">
                    <template slot-scope="scope">
                        <div>{{scope.row.remark || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="150"
                    prop="displayType"
                    label="展示样式">
                    <template slot-scope="scope">
                        <div>{{getchannelType(scope.row.displayType)}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="110"
                    prop="startDate"
                    sortable
                    label="排期">
                    <template slot-scope="scope">
                        <div style="text-align:center;">
                            <div v-if="scope.row.startDate">{{scope.row.startDate}}</div>
                            <div>-</div>
                            <div v-if="scope.row.endDate">{{scope.row.endDate}}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="status"
                    width="100"
                    label="状态">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status === 3" style="color: #67C23A;">首页展示中</div>
                        <div v-else-if="scope.row.status === 2" style="color: #909399;">暂停</div>
                        <div v-else-if="scope.row.status === 1">未展示</div>
                        <div v-else>测试</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="100"
                    label="关联专题">
                    <template slot-scope="scope">
                        <div>{{scope.row.topicTitle || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="创建人">
                    <template slot-scope="scope">
                        <div>{{scope.row.adminName || scope.row.adminId || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="110"
                    prop="updateTime"
                    sortable
                    label="修改时间">
                    <template slot-scope="scope">
                        <div>{{scope.row.updateTime || '-' | timeNo0}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="110"
                    prop="createTime"
                    sortable
                    label="创建时间">
                    <template slot-scope="scope">
                        <div>{{scope.row.createTime || '-' | timeNo0}}</div>
                    </template>
                </el-table-column>
                <template v-if="tableBox">
                    <el-table-column
                        prop="address"
                        fixed="right"
                        label="操作">
                        <template slot-scope="scope">
                            <el-button :disabled="scope.row._disabled" @click="checkChannel(scope.row)" type="text" size="small">关联</el-button>
                            <div><el-button @click="goDetail(scope.row.channelid)" type="text" size="small">查看详情</el-button></div>
                        </template>
                    </el-table-column>
                </template>
                <template v-else>
                    <el-table-column
                        fixed="right"
                        label="操作">
                        <template slot-scope="scope">
                            <div v-if="scope.row.status === 3 || scope.row.status === 1"><el-button type="text" size="small" @click="boforechangeStatus(scope.row, 2)" style="color: #909399;">暂停</el-button></div>
                            <div v-else><el-button type="text" size="small" @click="boforechangeStatus(scope.row, 1)" style="color: #67C23A;">上架</el-button></div>
                            <div><el-button type="text" size="small" @click="boforeDeleteItem(scope.row, scope.$index)" style="color: #F56C6C;">删除</el-button></div>
                            <div><el-button @click="goDetail(scope.row.channelid)" type="text" size="small">查看详情</el-button></div>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="[5, 10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 频道详情 -->
        <!-- <div class="channel-detail" v-if="showDetail">
            <detail @close="closeDetail" :channelId="detailId"/>
        </div> -->

        <el-dialog
        title="温馨提示"
        :visible.sync="dialogVisible">
        <span>此操作可能直接影响线上数据，您确定要执行此操作么？</span>
        <span slot="footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="changeStatus">确定修改</el-button>
        </span>
        </el-dialog>

        <el-dialog
        title="温馨提示"
        :visible.sync="dialogDeleteItem">
        <span>此操作可能直接影响线上数据，您确定要执行此操作么？</span>
        <span slot="footer">
            <el-button @click="dialogDeleteItem = false">取 消</el-button>
            <el-button type="primary" @click="deleteItem">确定删除</el-button>
        </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data () {
        return {
            keyWord: '', // 查询关键字
            data: [], // 总数据
            total: 0, // 数据总条数
            pageNum: 1,
            pageSize: this._pageSize,
            type: 0,
            status: 0,
            gameId: '',
            gameLoading: false,
            gameList: [],
            typeList: [
                { name: '精选', type: 1000 },
                { name: 'banner', type: 2000 },
                { name: 'banner+标题', type: 2100 },
                { name: 'banner+游戏信息', type: 3000 },
                { name: '轮播+标题', type: 4000 },
                { name: '轮播+游戏信息', type: 4100 },
                { name: '列表', type: 5000 },
                { name: 'icon陈列表', type: 7000 },
                { name: '游戏单陈列表', type: 10000 },
                { name: '游戏单icon轮播', type: 10100 },
                { name: '游戏单轮播', type: 10200 },
                { name: '游戏单精选', type: 10300 }
            ],
            loading: false,
            dialogVisible: false, // 切换状态弹窗
            dialogDeleteItem: false,
            detailId: 0 // 详情id
        }
    },
    props: [
        'tableBox', // 首页数据
        'channelType' // 样式类型 1000 2000
    ],

    computed: {
        tableData () {
            let start = this.pageNum * this.pageSize - this.pageSize
            let end = this.pageNum * this.pageSize
            return this.data.slice(start, end)
        }
    },
    created () {
        if (this.channelType) {
            this.pageSize = 5
            this.type = this.channelType
        }
        if (!this.$route.meta.keepAlive) {
            this.getDataList(1)
        }
    },

    activated () {
        if (this.$route.meta.keepAlive) {
            // 防止查询后输入keyword 导致返回时列表不一致
            if (this.listParams) {
                this.keyWord = this.listParams.keyword
            }
            this.getDataList(this.pageNum)
        }
    },

    methods: {
        // 查看列表
        async getDataList (num) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num
            this.listParams = {
                status: this.status
            }
            if (this.type) {
                this.listParams.type = this.type
            }
            if (this.keyWord) {
                this.listParams.keyword = this.keyWord
            }
            if (this.gameId) {
                this.listParams.gameId = this.gameId
            }
            let res = await this.$http.post('/home/findChannels', this.listParams)
            if (res.code === '0') {
                // 如果是首页要关联频道
                if (this.tableBox) {
                    let idList = []
                    this.tableBox.forEach(item => {
                        idList.push(item.channelid)
                    })
                    res.data.forEach(item => {
                        if (idList.includes(item.channelid)) {
                            item._disabled = true
                        } else {
                            item._disabled = false
                        }
                    })
                }
                res.data.forEach(item => {
                    item.startDate = item.startDate || ''
                    item.updateTime = item.updateTime || ''
                    item.createTime = item.createTime || ''
                })
                this.data = res.data
                this.total = res.data.length
            } else {
                this.$message({
                    message: res.desc,
                    type: 'error'
                })
            }
            this.loading = false
        },

        // 查询游戏
        filterMethod (res) {
            clearTimeout(this.timerId)
            if (!res) {
                this.gameLoading = false
                return
            }
            this.gameLoading = true
            this.timerId = setTimeout(() => {
                this.getgameList(res)
            }, 1000)
        },

        async getgameList (keyword) {
            let params = {
                keyword,
                pageIndex: 1,
                pageNum: 100
            }
            let res = await this.$http.post('/game/findGames', params)
            if (res.code === '0') {
                this.gameList = res.data
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.gameLoading = false
        },

        // 页容切换
        sizeChange (size) {
            this.pageSize = size
            this.pageNum = 1
        },

        // 页码切换
        pageChange (num) {
            this.pageNum = num
        },

        // 获取样式名称
        getchannelType (val) {
            for (let i = 0; i < this.typeList.length; i++) {
                let item = this.typeList[i]
                if (val === item.type) {
                    return item.name
                }
            }
            return val
        },

        // 关联频道
        checkChannel (item) {
            this.$emit('checkList', item)
        },

        // 查看详情
        goDetail (id) {
            if (id) {
                this.$router.push({
                    path: '/index/channelDetail',
                    query: {
                        id
                    }
                })
            } else {
                let query = {}
                if (this.channelType) {
                    query.type = this.channelType
                }
                this.$router.push({
                    path: '/index/channelDetail',
                    query
                })
            }
        },

        // 切换频道状态弹窗确认
        boforechangeStatus (item, status) {
            this.currentItem = item
            this.currentStatus = status
            this.dialogVisible = true
        },

        boforeDeleteItem (item, index) {
            this.currentItem = item
            this.dialogDeleteItem = true
        },

        // 切换频道状态
        async changeStatus () {
            let params = {
                channelId: this.currentItem.channelid,
                status: this.currentStatus
            }
            let res = await this.$http.post('/home/updateChannelStatus', params)
            if (res.code === '0') {
                this.currentItem.status = this.currentStatus
            } else {
                this.$message({
                    type: 'error',
                    message: '修改失败，请稍后再试'
                })
            }
            this.dialogVisible = false
        },

        // 切换频道状态
        async deleteItem () {
            let params = {
                channelId: this.currentItem.channelid
            }
            let res = await this.$http.post('/home/deleteChannel', params)
            if (res.code === '0') {
                this.data.forEach((item, index) => {
                    if (item.channelid === this.currentItem.channelid) {
                        this.data.splice(index, 1)
                    }
                })
                this.getDataList(this.pageNum)
            } else {
                this.$message({
                    type: 'error',
                    message: '删除失败，请稍后再试'
                })
            }
            this.dialogDeleteItem = false
        }

        /**
         * 同详情交互
         */
        // closeDetail () {
        //     this.showDetail = false
        //     this.getDataList()
        // }
    },

    beforeRouteLeave (to, from, next) {
        if (to.path === '/index/channelDetail') {
            from.meta.keepAlive = true
        } else {
            from.meta.keepAlive = false
        }
        next()
    }
}
</script>

<style lang="scss" scoped>
    .channel {
        .input-box {
            .btn {
                margin-left: 10px;
            }
            .select-title {
                font-size: 12px;
                font-weight: 400;
                margin-right: 5px;
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }

        .channel-detail {
            background-color: #ffffff;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 99;
        }
    }
</style>
